//
//  ViewController.m
//  CXDate
//
//  Created by xiaoma on 16/7/6.
//  Copyright © 2016年 xiaoma. All rights reserved.
//

#import "ViewController.h"
#import "NSDate+CX.h"

@interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (weak, nonatomic) IBOutlet UIPickerView *mainPickerView;

@property (weak, nonatomic) IBOutlet UILabel *dateLabel;

@property (strong, nonatomic) NSMutableArray *yearArrays;

@property (strong, nonatomic) NSMutableArray *monthArrays;

@property (strong, nonatomic) NSMutableArray *dayArrays;

@property (strong, nonatomic) NSMutableArray *hourArrays;

@property (copy, nonatomic) NSString *yearStr;

@property (copy, nonatomic) NSString *monthStr;

@property (copy, nonatomic) NSString *dayStr;

@property (strong, nonatomic) NSDateFormatter *dateFormatter;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dateFormatter = [[NSDateFormatter alloc] init];
    self.dateFormatter.dateFormat = @"yyyyMM";
    
    NSInteger allDays = [self totalDaysInMonth:[NSDate date]];
    for (int i = 1; i <= allDays; i++) {
        NSString *dayStr = [NSString stringWithFormat:@"%02i",i];
        [self.dayArrays addObject:dayStr];
    }
    
    //获取当前年
    NSInteger currentYear = [[NSDate date] getYear];
    NSString *yearStr = [NSString stringWithFormat:@"%04li",currentYear];
    NSInteger indexYear = [self.yearArrays indexOfObject:yearStr];
    if (indexYear == NSNotFound) {
        indexYear = 0;
    }
    [self.mainPickerView selectRow:indexYear inComponent:0 animated:YES];
    self.yearStr = self.yearArrays[indexYear];
    
    
    
    //获取当前日
    NSInteger currentDay = [[NSDate date] getDay];
    NSString *dayStr = [NSString stringWithFormat:@"%02li",currentDay];
    NSInteger indexDay = [self.dayArrays indexOfObject:dayStr];
    if (indexDay == NSNotFound) {
        indexDay = 0;
    }
    [self.mainPickerView selectRow:indexDay inComponent:2 animated:YES];
    self.dayStr = self.dayArrays[indexDay];
    
//    //获取当前日
//    NSInteger currentDay = [[NSDate date] getHour];
//    NSString *dayStr = [NSString stringWithFormat:@"%02li",currentDay];
//    NSInteger indexDay = [self.dayArrays indexOfObject:dayStr];
//    if (indexDay == NSNotFound) {
//        indexDay = 0;
//    }
//    [self.mainPickerView selectRow:indexDay inComponent:2 animated:YES];
//    self.dayStr = self.dayArrays[indexDay];
    
    //获取当前月份
    NSInteger currentMonth = [[NSDate date] getMonth];
    NSString *strMonth = [NSString stringWithFormat:@"%02li", currentMonth];
    NSInteger indexMonth = [self.monthArrays indexOfObject:strMonth];
    if (indexMonth == NSNotFound) {
        indexMonth = 0;
    }
    [self.mainPickerView selectRow:indexMonth inComponent:1 animated:YES];
    self.monthStr = self.monthArrays[indexMonth];
    
    [self updateLabelText];
    
}

#pragma mark - 计算当月多少天
- (NSInteger)totalDaysInMonth:(NSDate *)date {
    NSRange daysInOfMonth = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
    return daysInOfMonth.length;
}
#pragma mark - UIPickerViewDelegate/DataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 4;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return self.yearArrays.count;
    } else if (component == 1) {
        return self.monthArrays.count;
    } else  if (component == 2) {
        return self.dayArrays.count;
    } else {
        return self.hourArrays.count;
    }
        
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component __TVOS_PROHIBITED {
    if (component == 0) {
        return self.yearArrays[row];
    } else if (component == 1 ) {
        return self.monthArrays[row];
    } else if (component == 2 ) {
        return self.dayArrays[row];
    } else {
        return self.hourArrays[row];
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component __TVOS_PROHIBITED; {
    if (component == 0) {
        _yearStr = _yearArrays[row];
    } else if (component == 1 ) {
        _monthStr = _monthArrays[row];
    } else {
        _dayStr = _dayArrays[row];
    }
    
    [self updateLabelText];
    
    if (component != 2) {
        NSString *strDate = [NSString stringWithFormat:@"%@%@",_yearStr,_monthStr];
        [self upDateDayArraysWithDate:[self.dateFormatter dateFromString:strDate]];
    }
}

#pragma mark - label日期显示
- (void)updateLabelText {
    _dateLabel.text = [NSString stringWithFormat:@"%@年%@月%@日",_yearStr,_monthStr,_dayStr];
}

#pragma mark - 通过获取到的年月更新天数
- (void)upDateDayArraysWithDate:(NSDate *)currentDate {
    [_dayArrays removeAllObjects];
    
    NSInteger allDays = [self totalDaysInMonth:currentDate];
    for (int i = 1; i <= allDays; i++) {
        NSString *dayStr = [NSString stringWithFormat:@"%02i",i];
        [_dayArrays addObject:dayStr];
    }
    
    [self.mainPickerView reloadComponent:2];
    
    //获取当前日
    NSInteger indexDay = [self.dayArrays indexOfObject:_dayStr];
    if (indexDay == NSNotFound) {
        indexDay = (_dayArrays.count - 1) > 0 ? (self.dayArrays.count - 1) : 0;
    }
    [self.mainPickerView selectRow:indexDay inComponent:2 animated:YES];
    self.dayStr = self.dayArrays[indexDay];
    
    [self updateLabelText];
    
}

#pragma mark - Get
- (NSMutableArray *)yearArrays {
    if (!_yearArrays) {
        _yearArrays = [NSMutableArray array];
        
        for (int i = 1; i < 10000; i++) {
            NSString *yearStr = [NSString stringWithFormat:@"%04i",i];
            [_yearArrays addObject:yearStr];
        }
    }
    
    return _yearArrays;
}

- (NSMutableArray *)monthArrays {
    if (!_monthArrays) {
        _monthArrays = [NSMutableArray array];
        
        for (int i = 1; i <= 12; i++) {
            NSString *yearStr = [NSString stringWithFormat:@"%02i",i];
            [_monthArrays addObject:yearStr];
        }
    }
    
    return _monthArrays;
}

- (NSMutableArray *)hourArrays {
    if (!_hourArrays) {
        _hourArrays = [NSMutableArray array];
        
        for (int i = 1; i <= 24; i++) {
            NSString *yearStr = [NSString stringWithFormat:@"%02i",i];
            [_hourArrays addObject:yearStr];
        }
    }
    
    return _hourArrays;
}

- (NSMutableArray *)dayArrays {
    if (!_dayArrays) {
        _dayArrays = [NSMutableArray array];
    }
    
    return _dayArrays;
}
@end
